Zepto(function ($) {
    // 切换页面
    $(".index").click(function () {
        $(".in").each(function () {
            $(this).removeClass("in").addClass("out");
        });

        setTimeout(function () {
            $(".index_main,.index_next,.index_book,.index_support,.index_bg-top").hide();
        }, 1000);
        $(".page2_main").addClass("in");
    });
    var startPosition, endPosition, moveLength;

    $(document).bind('touchstart', function (e) {
        var touch = e.touches[0];
        startPosition = {
            y: touch.pageY
        }
    }).bind('touchmove', function (e) {
        var touch = e.touches[0];
        endPosition = {
            y: touch.pageY
        };
        moveLength = endPosition.y - startPosition.y;

        if (Math.abs(moveLength) > 60) {
            $(".in").each(function () {
                $(this).removeClass("in").addClass("out");
            });
            setTimeout(function () {
                $(".index_main,.index_next,.index_book,.index_support,.index_bg-top").hide();
            }, 1000);
            $(".page2_main").addClass("in");
        }
    });

    // 点击开始答题按钮
    $(".page2_btn_start").on("click", function () {
        var username = $("#vname").val();
        var company = $("#vunit").val();
        var mobile = $("#vmobile").val();
        if (!username) {
            alert('请填写姓名');
            return false;
        }
        if (!company) {
            alert('请填写单位');
            return false;
        }
        if (!mobile) {
            alert('请填写手机');
            return false;
        }
        var myreg = /^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (!myreg.test(mobile)) {
            alert('请输入有效的手机号码！');
            return false;
        }
        user_data = {
            'username': username,
            'company': company,
            'mobile': mobile,
        }
        var url = "/index/index/checkjoin";
        $.post(url, user_data, function (data) {
            if (data.code == 1) {
                $(".page2_main").hide();
                $(".page3_main").show();
                $(".page3_QA").eq(0).height($(window).height() - 167);
            } else {
                alert('每日最多答题3次,明天再来吧');
                return false;
            }
        });

    });


    var questionnaireID = $("#questionnaireID").val();
    var pageNumber = 1;

    $(".page3_A").on("click", "li", function () {
        var index = $(this).index();
        console.log(index);
        var r = $(this).parent().find(".page3_A-icon").eq(index);
        if (r.hasClass('selected')) {
            r.removeClass('selected');
        } else {
            r.addClass('selected');
        }
        // 显示题目正确与错误的图标
        // $(this).parent().children("li").children(".page3_A-text-textImg").children("._yes").css("display", "inline-block");
        // $(this).children(".page3_A-text-textImg").children(".page3_A-img").css("display", "inline-block");
    });

    // 点击下一题按钮显示下一题页面
    function showQuestion(pageNumber) {
        $(".page3_num span").html(pageNumber + "/10");
        $(".page3_Q").hide();
        $(".page3_A").hide();
        $("#question_" + pageNumber).show();
        $("#answer_" + pageNumber).show();
    }

    // 提交答案事件
    function submit() {
        var answer = "";
        var score = 0;
        $(".page3_A").each(function (index) {
            var selected = $(this).find(".selected");
            var val = selected.attr("answerValue");
            var rval = selected.attr("answerRight");
            if (val == rval) {
                score = score + 10;
            }
            answer = answer + val + ",";
        });
        var loadingStr = "得分" + score + "<br>排名计算中";
        var url = "/result/compute";
        var data = {
            "questionnaireID": questionnaireID,
            "answer": answer,
            "provinceID": provinceID
        };
        var submitTimeout = setTimeout(function () {
            window.location.href = "https://cdns1.dajiashequ.com/da19/errorv3.html";
        }, 10000);
        try {
            $.post(url, data, function (data, status) {
                clearTimeout(submitTimeout);
                if (data && data.result == "success") {
                    window.location.href = "/result/paper/";  // 结果页面的路径
                } else {
                    window.location.href = "https://cdns1.dajiashequ.com/da19/errorv3.html";  // 错误页面
                }
            });
        } catch (e) {
            alert("网络异常");
            window.location.href = "https://cdns1.dajiashequ.com/da19/errorv3.html";
        }
    }

    // 下一题（提交答案）按钮点击事件
    $("._next").on("click", function () {
        if ($(this).has('pagination_submit')) {
            $("._next").removeClass("pagination_submit").addClass("pagination_next");
        }
        if (pageNumber == 10) {
            submit();
            return;
        }
        pageNumber++;
        if (pageNumber == 10) {
            $("._next").removeClass("pagination_next").addClass("pagination_submit");
        }
        showQuestion(pageNumber);
        $(this).hide();
    });
});
